const app = getApp()

const videoContext = ''
Page({
    data: {
        xArticle: [] ,//疼痛课堂
        xProduct_list : [] , //产品列表
        page_num:1, //疼痛课堂页码
        userInfo:'', //用户信息
        toolList:[
          { title:'疼痛课堂', icon:'./image/1.png',url:'/subtool/pages/article/index/index'},
          { title:'疼痛量表', icon:'./image/2.png',url:'/subtool/pages/evaluate/index/index'},
          { title:'疼痛日记', icon:'./image/3.png',url:'/subtool/pages/dayremb/index/index'},
          { title:'康复里程碑', icon:'./image/4.png',url:'/subsale/pages/product/targetMilepost/index'},
          { title:'病友社区', icon:'./image/5.png',url:'/subtool/pages/community/index/index'},///subtool/pages/community/index/index
          { title:'患者故事', icon:'./image/6.png',url:''},///subtool/pages/article/index/index
        ],
        interviewOrder:[], //待完成访谈订单
        readList:[], //阅读
        test_result:'', //疼痛评估 使用人数
        xPageReady:true,
        RecommendedL:[],
        RecommendedR:[],
        page:1,//帖子页码
        warning:true,
        data:{}
    },
    onShareAppMessage: function (e) {
      if (e.from === 'button') {
        let id = e.target.id
        return {
          title: '国内首个慢性疼痛康复训练项目',
          path: '/subsale/pages/product/index/index?id='+id,
          imageUrl: 'https://7633-v3-92ah4-1300208657.tcb.qcloud.la/static/share_all.jpg',
        };
      }else{
        return {
          title: '疼痛久治不愈？还有一招',
          path: '/pages/navigation/index/index',
          imageUrl: 'https://7633-v3-92ah4-1300208657.tcb.qcloud.la/static/share_all.jpg',
        };
      }
    },
    onLoad:function () {
      let that =this;
      that.xUserInfo()
      that.xGetRecommended()
    },
    onReachBottom:function(){
      let that =this
      that.setData({
        warning: false
      })
      that.xGetRecommended()
    },
    xInfo:function(){

    },
    // 推荐的帖子
    xGetRecommended:function(){
      let that =this
      let page =that.data.page
      wx.cloud.callFunction({
        name: 'dynamic_content',
        data: {
            action: "get_recommended_content_list",
             page_number:page //页码
        }
      }).then(res => {
          let RecommendedL=[]
          let RecommendedR=[]
          for(let index in res.result){
            if(index & 1 === 1){
              that.data.RecommendedR.push(res.result[index])// 奇
            }else{
              that.data.RecommendedL.push(res.result[index])// 偶
            }
          }
          if (res.result.length === 0) {
            that.setData({
              page: page,
              warning: true,
              RecommendedL:that.data.RecommendedL,
              RecommendedR:that.data.RecommendedR,
              
            })
            setTimeout(() => {
              that.setData({
                warning: true
              })
            }, 2000)
          } else {
            page += 1
            that.setData({
              page: page,
              RecommendedL:that.data.RecommendedL,
              RecommendedR:that.data.RecommendedR,
            })
          }
      })
    },
    // 获取用户信息
    xUserInfo:function () {
      let that = this
      wx.cloud.callFunction({
        name : 'user' ,
        data : {
            action : 'detail'
        }
      }).then( res => {
        app.globalData.xCustomer = res.result
        app.globalData.user = res.result
        that.setData({
          userInfo:res.result,
          xPageReady:false
        })
      })
    },
    // 绑定渠道id 及医生id ||  渠道及医院
    user_channel:function(obj){
      let that =this;
      wx.cloud.callFunction({
        name: "user",
        data: {
          action: "login",
          userInfo: obj
        },
        success: function (res) {
          that.xUserInfo()
        }
      })
    },
    xParReload: async function () {
      let that = this
      that.setData({
        RecommendedR:[],
        RecommendedL:[]
      })
      let page = that.data.page
      let list = []
      for (var i = 0; i < page; i++) {
        await wx.cloud.callFunction({
          name: 'dynamic_content',
          data: {
            action: "get_recommended_content_list",
            page_number:i+1 //页码
          }
        }).then(res => {
          for(let index in res.result){
            if(index & 1 === 1){
              that.data.RecommendedR.push(res.result[index])// 奇
            }else{
              that.data.RecommendedL.push(res.result[index])// 偶
            }
          }
          if (res.result.length === 0) {
            that.setData({
              page: page,
              warning: true,
              RecommendedL:that.data.RecommendedL,
              RecommendedR:that.data.RecommendedR,
              
            })
            setTimeout(() => {
              that.setData({
                warning: true
              })
            }, 2000)
          } else {
            page += 1
            that.setData({
              page: page,
              RecommendedL:that.data.RecommendedL,
              RecommendedR:that.data.RecommendedR,
            })
          }
        })
      }
    },
    // 新版本首页初始化
    xInit:function(obj){
      // let that =this;
      // 疼痛课堂视频
      //  wx.cloud.callFunction({
      //   name: 'article',
      //   data: {
      //       action: "get_article_list",
      //       dataObj: obj
      //   }
      // }).then(res => {
      //   that.setData({
      //       xArticle : res.result
      //   })
      // })
      
      // 产品列表
      //  wx.cloud.callFunction({
      //   name: 'product',
      //   data: {
      //       action: "get_digital_therapy_product"
      //     }
      // }).then(res => {
      //   that.setData({
      //     xProduct_list:res.result
      //   })
      // })
      
    },
    // 疼痛课堂
    painclassMore:function(){
      wx.navigateTo({
        url: '/subtool/pages/article/index/index', //painclass
      })
    },
    // 正念与放松
    xToRelaxation:function(){
      wx.navigateTo({
        url: '/subtool/pages/rudio/mindfulness/index',
      })
    },
    // 视频全屏播放
    bindplay:function(e){
      let index = e.currentTarget.dataset.index
      this.videoContext = wx.createVideoContext(index+'video')
      this.videoContext.requestFullScreen({ direction: 0 })
    },
    // 康复训练
    xTotraining:function(){
      wx.navigateTo({
        url: '/subtool/pages/training/index/index',
      })
    },
    // 发布帖子
    xToCreate: function () {
      let that = this
      wx.navigateTo({
          url: '/subtool/pages/community/add/index?pageType=index',
      })
    },
    // 工具
    xToNext:function(e){
      let that =this
      let path = e.currentTarget.dataset.path
      // if(path == '/subsale/pages/product/targetMilepost/index'){
      //   if(!app.globalData.product_buy){
      //     wx.navigateTo({
      //       url: path,
      //     })
      //   }else{
      //     wx.showModal({
      //       title: "提示",
      //       content: "解锁完整版可使用该功能",
      //       confirmText:'去解锁',
      //       success (res) {
      //         if (res.confirm) {
      //           that.xToProductde()
      //         }
      //       }
      //     })
      //   }
      // }else
      if(path==''){
          wx.showToast({
            title: '模块开发中,敬请期待',
            icon:'none'
          })
      }else{
        wx.navigateTo({
          url: path,
        })
      }
    },
    // 产品详情
    xToProductde:function(){
      let that =this
      if(!app.globalData.user.phone_number){
        let path = '/subsale/pages/product/index/index'
        let params = '&choose=true&id=26da8e4962b93a6000ec7c3d68d9d3eb'
        wx.navigateTo({
          url: '/pages/navigation/login/index?path='+path +params,
        })
      }else{
        wx.navigateTo({
          url: '/subsale/pages/product/index/index?choose=true&id=26da8e4962b93a6000ec7c3d68d9d3eb'
        })
      }
    },
    xToProductOther:function(e){
      let name = e.currentTarget.dataset.name
      wx.navigateTo({
        url: '/subtool/pages/productother/index/index?name='+name,
      })
    },
    // 用户调研
    xToResearch:function(e){
        let type =e.currentTarget.dataset.type
        let title =e.currentTarget.dataset.title
        wx.navigateTo({
          url: '/subuser/pages/research/index?type='+type+'&title='+title,
        })
    },
    // 产品详情
    xToProduct:function(e){
      let params = e.currentTarget.dataset
      wx.navigateTo({
        url: '/subsale/pages/product/index/index?id='+ params.id ,
      })
    },
    xToProductS:function(){
      let that =this;
      let buystatus = that.data.interviewOrder.length>0?true:false
      wx.navigateTo({
        url: '/subsale/pages/product/index/index?id=617ef50c622575a00a1ea51026fc3837&buystatus='+ buystatus ,
      })
    },
    xToCs:function(){
      wx.navigateTo({
        url: '/subtool/pages/community/list/index',
      })
    },
    // 联系客服
    xMessage:function(){
      var url= 'https://7633-v3-92ah4-1300208657.tcb.qcloud.la/static/cell.png?time='+Date.now() //地址加上时间戳，否则从缓存中加载
      wx.previewImage({
        urls: [url],
      })
    },
    // 阅读详情
    xToRead:function(e){
      let that = this
      let param = e.currentTarget.dataset
      // 文章
      if(e.type =='1'){
        wx.navigateTo({
          url: '/subtool/pages/article/detail/index?id=' + param.id,
        })
      }
    },
    
    // 前往文章主页
    xToArticle: function () {
        wx.navigateTo({
            url: '/subtool/pages/article/index/index',
        })
    },

    // 前往文章详情
    xToArticleDetail: function (e) {
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '/subtool/pages/article/detail/index?id=' + id,
        })
    },
    // 前往心里测评
    xToEvaluate:function(){
      // if(this.data.userInfo.phone_number){
        wx.navigateTo({
          url: '/subuser/pages/improveData/index',
        })
      // }else{
      //   let path ='/subuser/pages/improveData/index'
      //   wx.navigateTo({
      //     url: '/pages/navigation/login/index?path='+ path,
      //   })
      // }
    },
    //前往讨论详情
    xToDetail: function (e) {
      let id = e.currentTarget.dataset.id
      wx.navigateTo({
        url: '/subtool/pages/community/detail/index?id=' + id ,
      })
    },
   
})